//Examples/galois_01.cpp

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "jerasure.h"
#include "galois.h"

#define talloc(type, num) (type *) malloc(sizeof(type)*(num))

void usage(char *s)
{
	fprintf(stderr, "usage: galois_01 w - Shows galois_wXX_region_multby_2\n");
	fprintf(stderr, "       \n");
	fprintf(stderr, "       w must be 8, 16 or 32.  Sets up an array of 4 random words in\n");
	fprintf(stderr, "       GF(2^w) and multiplies them by two.  \n");
	fprintf(stderr, "       \n");
	fprintf(stderr, "This demonstrates: galois_w08_region_multby_2()\n");
	fprintf(stderr, "                   galois_w16_region_multby_2()\n");
	fprintf(stderr, "                   galois_w32_region_multby_2()\n");
	if (s != NULL) fprintf(stderr, "%s\n", s);
	exit(1);
}

int main(int argc, char **argv)
{
	unsigned char *x, *y;
	uint16_t *xs, *ys;
	uint32_t *xi, *yi;
	int *a32, *copy;
	int i;
	int w;

	if (argc != 2) usage(NULL);
	if (sscanf(argv[1], "%d", &w) == 0 || (w != 8 && w != 16 && w != 32)) usage((char *)"Bad w");

	srand48(0);
	a32 = new int [4];
	copy = new int [4];
	y = (unsigned char *) a32;
	for (i = 0; i < 4*sizeof(int); i++) y[i] = lrand48()%255;
	memcpy(copy, a32, sizeof(int)*4);

	if (w == 8) {
		x = (unsigned char *) copy;
		y = (unsigned char *) a32;
		galois_w08_region_multby_2((unsigned char *) a32, sizeof(int)*4);
		for (i = 0; i < 4*sizeof(int)/sizeof(char); i++) {
			printf("Char %2d: %3u *2 = %3u\n", i, x[i], y[i]);
		}
	} else if (w == 16) {
		xs = (unsigned short *) copy;
		ys = (unsigned short *) a32;
		galois_w16_region_multby_2((unsigned char *) a32, sizeof(int)*4);
		for (i = 0; i < 4*sizeof(int)/sizeof(uint16_t); i++) {
			printf("Short %2d: %5u *2 = %5u\n", i, xs[i], ys[i]);
		}
	} else if (w == 32) {
		xi = (unsigned int *) copy;
		yi = (unsigned int *) a32;
		galois_w32_region_multby_2((unsigned char *) a32, sizeof(int)*4);
		for (i = 0; i < 4*sizeof(int)/sizeof(uint32_t); i++) {
			printf("Int %2d: %10u *2 = %10u\n", i, xi[i], yi[i]);
		}
	} 
}
